import {
    NativeModules,
    NativeEventEmitter,
    Platform
} from 'react-native'

const {
    RNXG:rnxg
} = NativeModules
const eventEmitter = new NativeEventEmitter(rnxg);

function resolve(data) {
    return Promise.resolve(data);
}

function reject(){
    return Promise.reject("This method is not supported on this platform");
}

function unwrap(notification){
    let content = null;
    if(notification){
        content = notification.custom;
        if(typeof(content) == 'string'){
            try{
                content = JSON.parse(content)
            }catch(err){
                console.log(err);
            }
        }
    }

    return content;
}

export default class RNXGManager {

    /**
     * 是否开启debug模式,正式发布必须设置为false
     * true:开启,false:关闭。
     * @param {boolean} debugMode
     */
    static enableDebug(debugMode) {
        rnxg.enableDebug(debugMode)
    }

    /**
     * 配置accessId
     * @param {long} accessId
     */
    static setAccessId(accessId) {
        rnxg.setAccessId(accessId)
    }

    /**
     * 配置accessKey
     * @param {string} accessKey
     */
    static setAccessKey(accessKey) {
        rnxg.setAccessKey(accessKey)
    }

    /**
     *
     * 获取设备的token，只有注册成功才能获取到正常的结果
     */
    static getToken() {
        return rnxg.getToken();
    }

    /**
     * 设置上报通知栏是否关闭 默认打开
     * Android Only
     * @param {boolean} debugMode
     */
    static setReportNotificationStatusEnable(debugMode) {
        return reject();
    }

    /**
     * 设置上报APP 列表，用于智能推送 默认打开
     * Android Only
     * @param {boolean} debugMode
     */
    static setReportApplistEnable(debugMode) {
        return reject();
    }

    /**
     * 设置支持第三方厂商推送
     * Android Only
     * @param {boolean} flag
     */
    static enableOtherPush(flag) {
        return reject();
    }

    /**
     * 设置小米推送APPID
     * Android Only
     * @param {string} appid
     */
    static setMiPushAppId(appid) {
        return reject();
    }

    /**
     * 设置小米推送APPKEY
     * Android Only
     * @param {string} appkey
     */
    static setMiPushAppKey(appkey) {
        return reject();
    }

    /**
     * 设置魅族推送APPID
     * Android Only
     * @param {string} appid
     */
    static setMzPushAppId(appid) {
        return reject();
    }

    /**
     * 设置魅族推送APPKEY
     * Android Only
     * @param {string} appkey
     */
    static setMzPushAppKey(appkey) {
        return reject();
    }
    /**
     * 华为手机的写日志定位问题
     * Android Only
     * @param {boolean} isHuaweiDebug
     */
    static setHuaweiDebug(isHuaweiDebug) {
        return reject();
    }

    /**
     * 启动并注册
     */
    static registerPush() {
        rnxg.registerPush()
    }

    /**
     * 反注册，建议在不需要接收推送的时候调用
     */
    static unregisterPush() {
        rnxg.unregisterPush()
    }

    static getLaunchAppNotification() {
        return rnxg.getLaunchAppNotification().then(notification => unwrap(notification));
    }

    static notifyJsDidLoad() {
        return resolve(0);
    }

    static addListener(name,cb) {
        return eventEmitter.addListener(name, cb);
    }

    static removeListener(name, cb) {
        return eventEmitter.removeListener(name, cb);
    }

    static popDeliveredNotifications() {
        return rnxg.popDeliveredNotifications().then(notifications => notifications.map(notification => unwrap(notification)));
    }

    static popCachedNotifications() {
        return rnxg.popCachedNotifications().then(notifications => notifications.map(notification => unwrap(notification)));
    }

    static removeDeliveredNotifications() {
        return rnxg.removeDeliveredNotifications();
    }

    /**
     * 注册回调
     */
    static onRegister(cb) {
        return this.addListener(
            rnxg.RNXG_ON_UNREGISTER,
            cb
        )
    }

    /**
     * 反注册回调
     */
    static onUnregister(cb) {
        return this.addListener(
            rnxg.RNXG_ON_REGISTER,
            cb
        )
    }

    /**
     * 通知被点击触发的回调
     */
    static onNotificationClick(cb) {
        return this.addListener(
            rnxg.RNXG_NOTIFICATION_ON_CLICK,
            notification => {
                cb(unwrap(notification));
            }
        )
    }

    /**
     * 通知被展示触发的回调，可以在此保存APP收到的通知
     */
    static onNotificationReceive(cb) {
        return this.addListener(
            rnxg.RNXG_NOTIFICATION_ON_RECEIVE,
            notification => {
                cb(unwrap(notification));
            }
        )
    }

}
/* 
const RNXG = new RNXGManager();

export default RNXG; */